<?php
include_once('common/header.php');			
if($AppUI->id <=0 || !isset($_SERVER['HTTP_REFERER'])) {
	Vfriend_Function::redirect(HOST_VFRIEND);
}
$arrParams 		= $objRequest->getParams();
$strRefer		= isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$arrFortune		= isset($arrParams['fortuneid']) ? $arrParams['fortuneid'] : null;
$intProductId	= isset($arrParams['productid']) ? $arrParams['productid'] : 0;
$strToken		= isset($arrParams['token']) ? $arrParams['token'] : '';
$strTok			= $AppUI->id . '_' . $intProductId;
$strSession		= isset($_SESSION['listFortune' . $strTok]) ? $_SESSION['listFortune' . $strTok] : '';
$intError		= 0;
if($AppUI->id && $intProductId && $arrFortune && $strToken != '' && $strSession != '' && $strToken == $strSession && Vfriend_Function::isValidLink($strRefer)) {
	unset($_SESSION['listFortune' . $strTok]);
	$objProduct		= new Product();
	$strProductName	= $objProduct->getName($intProductId);
	$intCountSell	= 0;
	foreach($arrFortune as $value) {
		$intErrorR 	= $objProduct->sellProduct($AppUI->id,$intProductId,$value);
		if($intErrorR == 0) $intCountSell ++;
	}
	$strMessage	= $arrMessage[4];
	$strMessage	= str_replace('[strProductName]', $strProductName, $strMessage);
	$strMessage	= str_replace('[intQuantity]', $intCountSell, $strMessage);
	Vfriend_Function::setMsg($strMessage);
} else {
	$intError	= 11;
}
if($intError > 0) {
	$strError = $arrError[$intError];
	Vfriend_Function::setError($strError);
}
$strRefer	= $strRefer ? $strRefer : Vfriend_Function::linkFullhouseForFortune($AppUI->username, $AppUI->hex);
Vfriend_Function::redirect($strRefer);
?>